package com.newtype.meeting.client.widget.backend;

import java.util.Arrays;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGrid;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridCellRenderer;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.MeetingCategory;

public class MeetingCategoryPanel extends ContentPanel {

	public MeetingCategoryPanel(TreeStore<ModelData> store) {
		setHeading("类别维护 [点击进行维护]");
		setSize(600, 500);
		setLayout(new FlowLayout());

		ToolBar toolBar = new ToolBar();
		toolBar.add(getNewMainButton());
		toolBar.add(new SeparatorToolItem());
		toolBar.add(getNewNextButton());

		ColumnConfig cateName = new ColumnConfig("categoryName", "会议类别", 200);
		cateName.setRenderer(new TreeGridCellRenderer<ModelData>());
		ColumnModel cm = new ColumnModel(Arrays.asList(cateName));

		TreeGrid<ModelData> tree = new TreeGrid<ModelData>(store, cm);
		tree.setBorders(false);
		tree.getStyle().setLeafIcon(IconHelper.createStyle("icon-category"));
		tree.setAutoHeight(true);
		tree.setAutoExpandColumn("categoryName");
		tree.setTrackMouseOver(false);

		GridSelectionModel<ModelData> sm = new GridSelectionModel<ModelData>() {
			@Override
			protected void handleMouseClick(GridEvent<ModelData> e) {
				if (e.getModel().get("parentId") == null) {
					Dispatcher.forwardEvent(AppEvents.ToMainCategoryWindow, e
							.getModel());
				} else {
					Dispatcher.forwardEvent(AppEvents.ToNextCategoryWindow, e
							.getModel());
				}
			}
		};
		tree.setSelectionModel(sm);

		add(toolBar);
		add(tree);
	}

	private Button getNewNextButton() {
		Button button = new Button("新增次类别");
		button.setIconStyle("icon-add");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToNextCategoryWindow,
						new MeetingCategory());
			}
		});
		return button;
	}

	private Button getNewMainButton() {
		Button button = new Button("新增主类别");
		button.setIconStyle("icon-add");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToMainCategoryWindow,
						new MeetingCategory());
			}
		});
		return button;
	}

}
